<?
import("Zend_Http_Client");
import("stock_analyser_AnalyserView");
import("stock_price_PriceConfig");
class stock_price_LivePriceView extends jsml_View {
	public function import() {
		//<@ $this->jsonp();
		$market = "ho";
		$httpClient = new Zend_Http_Client('http://banggia.dag.vn/hose/');		
		$httpClient->setHeaders(array(
			'Accept'=>'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
			'Accept-Charset'=>'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
			'Accept-Encoding'=>'gzip,deflate,sdch',
			'Accept-Language'=>'en-US,en;q=0.8',
			'Cache-Control'=>'max-age=0',
			'Connection'=>'keep-alive',
			'Host'=>'banggia.dag.vn',
			'User-Agent'=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10'
		));
		$html = $httpClient->request('GET')->getBody();
		
		$html = substr($html, strpos($html, "<tr class='rowclass' height=22px"));
		$html = substr($html, 0, strpos($html, '<tr  id ="remainedline" class="splitLine" >'));
		
		$html = '<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/></head><body><table>'.$html.'</table></body></html>';
		//echo $html;
		$doc = new DOMDocument();
		$old_E = error_reporting(E_ERROR);			
		$doc->loadHTML($html);
		error_reporting($old_E);
		
		$trNodeList = $doc->getElementsByTagName("table")->item(0)->getElementsByTagName("tr");
		foreach($trNodeList as $i=>$trNode){
			$livePriceDt = array("__new__"=>1);
			$tdNodeList = $trNode->getElementsByTagName("td");
			foreach($tdNodeList as $i2=>$tdNode){
				$text = trim($tdNode->textContent);
				if($i2 == 0){
					$text = $obj2->find("span.symbolclass")->text();
					$livePriceDt[$this->vo->stock_symbol] = $text;
					$text = $date;
					$livePriceDt[$this->vo->stockDate] = $this->toDate($text);
				}
				else if($i2 == 1) $livePriceDt[$this->vo->ceil] = $this->toPrice($text);
				else if($i2 == 2) $livePriceDt[$this->vo->floor] = $this->toPrice($text);
				else if($i2 == 3) $livePriceDt[$this->vo->ref] = $this->toPrice($text);

				else if($i2 == 4) $livePriceDt[$this->vo->buyVolumeP] = $this->toVolume($text);

				else if($i2 == 5) $livePriceDt[$this->vo->buyPrice3] = $this->toPrice($text);
				else if($i2 == 6) $livePriceDt[$this->vo->buyVolume3] = $this->toVolume($text);
				else if($i2 == 7) $livePriceDt[$this->vo->buyPrice2] = $this->toPrice($text);
				else if($i2 == 8) $livePriceDt[$this->vo->buyVolume2] = $this->toVolume($text);
				else if($i2 == 9) $livePriceDt[$this->vo->buyPrice1] = $this->toPrice($text);
				else if($i2 == 10) $livePriceDt[$this->vo->buyVolume1] = $this->toVolume($text);

				else if($i2 == 17) $livePriceDt[$this->vo->sellPrice1] = $this->toPrice($text);
				else if($i2 == 18) $livePriceDt[$this->vo->sellVolume1] = $this->toVolume($text);
				else if($i2 == 19) $livePriceDt[$this->vo->sellPrice2] = $this->toPrice($text);
				else if($i2 == 20) $livePriceDt[$this->vo->sellVolume2] = $this->toVolume($text);
				else if($i2 == 21) $livePriceDt[$this->vo->sellPrice3] = $this->toPrice($text);
				else if($i2 == 22) $livePriceDt[$this->vo->sellVolume3] = $this->toVolume($text);

				else if($i2 == 23) $livePriceDt[$this->vo->sellVolumeP] = $this->toVolume($text);

				else if($i2 == 26){
					if($market == "ho") $livePriceDt[$this->vo->open] = $this->toPrice($text);
					else if($market == "ha") $livePriceDt[$this->vo->average] = $this->toPrice($text);
				}
				else if($i2 == 24) $livePriceDt[$this->vo->high] = $this->toPrice($text);
				else if($i2 == 25) $livePriceDt[$this->vo->low] = $this->toPrice($text);
				else if($i2 == 14) $livePriceDt[$this->vo->close] = $this->toPrice($text);
				else if($i2 == 16) $livePriceDt[$this->vo->volume] = $this->toVolume($text);
				else if($i2 == 15) $livePriceDt[$this->vo->volume0] = $this->toVolume($text);

				else if($i2 == 27){
					$livePriceDt[$this->vo->buyVolumeF] = $this->toVolume($text);
					if($market == "ho") $livePriceDt[$this->vo->sellVolumeF] = null;
				}
				else if($i2 == 28){
					if($market == "ha") $livePriceDt[$this->vo->sellVolumeF] = $this->toVolume($text);
				}
				if($i2 == 27 || $i2 == 28){
					if($livePriceDt[$this->vo->open] == null) $livePriceDt[$this->vo->open] = $livePriceDt[$this->vo->ref];
					if($livePriceDt[$this->vo->high] == null) $livePriceDt[$this->vo->high] = $livePriceDt[$this->vo->ref];
					if($livePriceDt[$this->vo->low] == null) $livePriceDt[$this->vo->low] = $livePriceDt[$this->vo->ref];
					if($livePriceDt[$this->vo->average] == null) $livePriceDt[$this->vo->average] = null;
					if($livePriceDt[$this->vo->volume] == null) $livePriceDt[$this->vo->volume] = 0;

					$symbol = $livePriceDt[$this->vo->stock_symbol];
					if($this->isEqual($livePriceDt, $this->livePriceDtMap[$symbol]) == false){
						$this->livePriceDtMap[$symbol] = $livePriceDt;
						$this->livePriceDtMapSave[$symbol] = $livePriceDt;
					}
				}
			}
			print_p($livePriceDt);
		}
		//@>
	}
	
	public $buttonSave;
	public $iframeHo;
	public $iframeHa;
	public $livePriceDtMap = array("__new__"=>1);
	public $livePriceDtMapSave = array("__new__"=>1);
	public function view() {
		$this->div->css("width", $this->screenW - 17);
		$this->div->css("height", $this->screenH - 4);
		//$this->a("AAAAA", "BBBBB"); return $this;
		//$this->queryCreate();
		$this->buttonSave = _('<button>Save</button>')->appendTo($this->div)->css($this->config->getCss(null, array(left=>1200,top=>0, width=>50, height=>25)));
		$this->buttonSave->bind("click.buttonSave", function($event){
			$this->livePriceDtMapSave = array("__new__"=>1);
			foreach (array("ho", "ha") as $_i=>$market) {
				if($market=="ho"){
					$table = $this->iframeHo->contents()->find("#tableQuotes > tbody");
					$date = $this->toDate($this->iframeHo->contents()->find("#TimePanel")->text());
					$trIndex = $this->iframeHo->contents()->find("#vnindex");
					$symbolIndex = "VNINDEX";
				} else if($market=="ha"){
					$table = $this->iframeHa->contents()->find("#tableQuotes > tbody");
					$date = $this->toDate($this->iframeHa->contents()->find("#TimePanel")->text());
					$trIndex = $this->iframeHa->contents()->find("#hnxindex");
					$symbolIndex = "HAINDEX";
				}

				$livePriceDt = array("__new__"=>1);
				$livePriceDt[$this->vo->market] = $market;
				$livePriceDt[$this->vo->stock_symbol] = $symbolIndex;
				$livePriceDt[$this->vo->stockDate] = $date;
				$trIndex->children()->each(function($i2, $obj2){
					$obj2 = _($obj2);
					$text = trim($obj2->text());
					if($i2 == 1) $livePriceDt[$this->vo->close] = $this->toPrice($text);
					else if($i2 == 4) $livePriceDt[$this->vo->ref] = round( $livePriceDt[$this->vo->close] - $this->toPrice(substr($text,1)) , 2);
					else if($i2 == 8) $livePriceDt[$this->vo->volume] = $this->toVolume($text)/10;
				});
				$livePriceDt[$this->vo->open] = $livePriceDt[$this->vo->ref];
				if($livePriceDt[$this->vo->open] > $livePriceDt[$this->vo->close]) {
					$livePriceDt[$this->vo->high] = $livePriceDt[$this->vo->open];
					$livePriceDt[$this->vo->low] = $livePriceDt[$this->vo->close];
				} else {
					$livePriceDt[$this->vo->high] = $livePriceDt[$this->vo->close];
					$livePriceDt[$this->vo->low] = $livePriceDt[$this->vo->open];
				}
				
				$livePriceDt[$this->vo->ceil] = $livePriceDt[$this->vo->high];
				$livePriceDt[$this->vo->floor] = $livePriceDt[$this->vo->low];
				$livePriceDt[$this->vo->average] = $livePriceDt[$this->vo->close];
				
				$livePriceDt[$this->vo->buyVolumeP] = null;

				$livePriceDt[$this->vo->buyPrice3] = null;
				$livePriceDt[$this->vo->buyVolume3] = null;
				$livePriceDt[$this->vo->buyPrice2] = null;
				$livePriceDt[$this->vo->buyVolume2] = null;
				$livePriceDt[$this->vo->buyPrice1] = null;
				$livePriceDt[$this->vo->buyVolume1] = null;

				$livePriceDt[$this->vo->sellPrice1] = null;
				$livePriceDt[$this->vo->sellVolume1] = null;
				$livePriceDt[$this->vo->sellPrice2] = null;
				$livePriceDt[$this->vo->sellVolume2] = null;
				$livePriceDt[$this->vo->sellPrice3] = null;
				$livePriceDt[$this->vo->sellVolume3] = null;

				$livePriceDt[$this->vo->sellVolumeP] = null;
							
				if($this->isEqual($livePriceDt, $this->livePriceDtMap[$symbolIndex]) == false){
					$this->livePriceDtMap[$symbolIndex] = $livePriceDt;
					$this->livePriceDtMapSave[$symbolIndex] = $livePriceDt;
				}
				//$__console->log($this->livePriceDtMap[$symbolIndex]);
					
				$table->children()->each(function($i, $obj) {
					$obj = _($obj);
					if($obj->attr("class") == "rowclass"){
						//if($i!=2) return;
						$livePriceDt = array("__new__"=>1);
						$obj->children()->each(function($i2, $obj2){
							$obj2 = _($obj2);
							$text = trim($obj2->text());
							//$__console->log($i2.":".$text);
							if($i2 == 0){
								$text = $obj2->find("span.symbolclass")->text();
								$livePriceDt[$this->vo->stock_symbol] = $text;
								$livePriceDt[$this->vo->stockDate] = $date;
								$livePriceDt[$this->vo->market] = $market;
							}
							else if($i2 == 1) $livePriceDt[$this->vo->ceil] = $this->toPrice($text);
							else if($i2 == 2) $livePriceDt[$this->vo->floor] = $this->toPrice($text);
							else if($i2 == 3) $livePriceDt[$this->vo->ref] = $this->toPrice($text);

							else if($i2 == 4) $livePriceDt[$this->vo->buyVolumeP] = $this->toVolume($text);

							else if($i2 == 5) $livePriceDt[$this->vo->buyPrice3] = $this->toPrice($text);
							else if($i2 == 6) $livePriceDt[$this->vo->buyVolume3] = $this->toVolume($text);
							else if($i2 == 7) $livePriceDt[$this->vo->buyPrice2] = $this->toPrice($text);
							else if($i2 == 8) $livePriceDt[$this->vo->buyVolume2] = $this->toVolume($text);
							else if($i2 == 9) $livePriceDt[$this->vo->buyPrice1] = $this->toPrice($text);
							else if($i2 == 10) $livePriceDt[$this->vo->buyVolume1] = $this->toVolume($text);

							else if($i2 == 17) $livePriceDt[$this->vo->sellPrice1] = $this->toPrice($text);
							else if($i2 == 18) $livePriceDt[$this->vo->sellVolume1] = $this->toVolume($text);
							else if($i2 == 19) $livePriceDt[$this->vo->sellPrice2] = $this->toPrice($text);
							else if($i2 == 20) $livePriceDt[$this->vo->sellVolume2] = $this->toVolume($text);
							else if($i2 == 21) $livePriceDt[$this->vo->sellPrice3] = $this->toPrice($text);
							else if($i2 == 22) $livePriceDt[$this->vo->sellVolume3] = $this->toVolume($text);

							else if($i2 == 23) $livePriceDt[$this->vo->sellVolumeP] = $this->toVolume($text);

							else if($i2 == 26){
								if($market == "ho") $livePriceDt[$this->vo->open] = $this->toPrice($text);
								else if($market == "ha") $livePriceDt[$this->vo->average] = $this->toPrice($text);
							}
							else if($i2 == 24) $livePriceDt[$this->vo->high] = $this->toPrice($text);
							else if($i2 == 25) $livePriceDt[$this->vo->low] = $this->toPrice($text);
							else if($i2 == 14) $livePriceDt[$this->vo->close] = $this->toPrice($text);
							else if($i2 == 16) $livePriceDt[$this->vo->volume] = $this->toVolume($text);
							else if($i2 == 15) $livePriceDt[$this->vo->volume0] = $this->toVolume($text);

							else if($i2 == 27){
								$livePriceDt[$this->vo->buyVolumeF] = $this->toVolume($text);
								if($market == "ho") $livePriceDt[$this->vo->sellVolumeF] = null;
							}
							else if($i2 == 28){
								if($market == "ha") $livePriceDt[$this->vo->sellVolumeF] = $this->toVolume($text);
							}
							if($i2 == 27 || $i2 == 28){
								if($livePriceDt[$this->vo->close] == null) $livePriceDt[$this->vo->close] = $livePriceDt[$this->vo->ref];
								if($livePriceDt[$this->vo->open] == null) $livePriceDt[$this->vo->open] = $livePriceDt[$this->vo->ref];
								if($livePriceDt[$this->vo->high] == null) $livePriceDt[$this->vo->high] = $livePriceDt[$this->vo->ref];
								if($livePriceDt[$this->vo->low] == null) $livePriceDt[$this->vo->low] = $livePriceDt[$this->vo->ref];
								if($livePriceDt[$this->vo->average] == null) $livePriceDt[$this->vo->average] = null;
								if($livePriceDt[$this->vo->volume] == null) $livePriceDt[$this->vo->volume] = 0;
								if($livePriceDt[$this->vo->buyPrice1] == 0) $livePriceDt[$this->vo->buyPrice1] = $livePriceDt[$this->vo->close];
								if($livePriceDt[$this->vo->sellPrice1] == 0) $livePriceDt[$this->vo->sellPrice1] = $livePriceDt[$this->vo->close];

								$symbol = $livePriceDt[$this->vo->stock_symbol];
								if($this->isEqual($livePriceDt, $this->livePriceDtMap[$symbol]) == false){
									$this->livePriceDtMap[$symbol] = $livePriceDt;
									$this->livePriceDtMapSave[$symbol] = $livePriceDt;
								}
							}
						});
					}
				});
			}
			//alert(JSON::stringify($this->livePriceDtMap));
			if(count($this->livePriceDtMapSave)>=0){
				$this->queryReplaceMap($this->livePriceDtMapSave);
			}
		});
		?>
<$html>
<iframe src="http://banggia.dag.vn/hose/"></iframe>
</$html>
		<?
		//http://127.0.0.1:99/ck2/web/hose/
		//http://banggia.dag.vn/hose/
		//http://127.0.0.1:99/ck2/web/banggia.dag.vn/hose/
		$this->iframeHo = _($html)->appendTo($this->div)->css($this->config->getCss(null,array(left=>0,top=>0, width=>$this->div->width(), height=>$this->div->height()/2, "overflow" => "scroll")));
		$hoReady = false;
		$haReady = false;
		$this->iframeHo->load(function (){
			$hoReady = true;
			if($hoReady && $haReady){
				//$this->buttonSave->click();
				$hoReady = null;
				$haReady = null;
			}
		});

		?>
<$html>
<iframe src="http://banggia.dag.vn/hnx/hnx_full.jsp"></iframe>
</$html>
		<?
		//http://127.0.0.1:99/ck2/web/hnx/hnx_full.jsp
		//http://banggia.dag.vn/hnx/hnx_full.jsp
		//http://127.0.0.1:99/ck2/web/banggia.dag.vn/hnx/hnx_full.jsp
		$this->iframeHa = _($html)->appendTo($this->div)->css($this->config->getCss(null,array(left=>0,top=>$this->div->height()/2, width=>$this->div->width(), height=>$this->div->height()/2, "overflow" => "scroll")));
		$this->iframeHa->load(function (){
			$haReady = true;
			if($hoReady && $haReady){
				//$this->buttonSave->click();
				$hoReady = null;
				$haReady = null;
			}
		});
		setInterval(function(){
			$this->buttonSave->click();	
		},2000);
		setTimeout("location.reload(true);",1000*60);
		return $this;
	}
	
	public function aaa(){
		$this->livePriceDtMapSave = array("__new__"=>1);
		foreach (array("ho", "ha") as $_i=>$market) {
			if($market=="ho"){
				$table = $this->iframeHo->contents()->find("#tableQuotes > tbody");
				$date = $this->toDate($this->iframeHo->contents()->find("#TimePanel")->text());
				$trIndex = $this->iframeHo->contents()->find("#vnindex");
				$symbolIndex = "VNINDEX";
			} else if($market=="ha"){
				$table = $this->iframeHa->contents()->find("#tableQuotes > tbody");
				$date = $this->toDate($this->iframeHa->contents()->find("#TimePanel")->text());
				$trIndex = $this->iframeHa->contents()->find("#hnxindex");
				$symbolIndex = "HAINDEX";
			}

			$livePriceDt = array("__new__"=>1);
			$livePriceDt[$this->vo->market] = $market;
			$livePriceDt[$this->vo->stock_symbol] = $symbolIndex;
			$livePriceDt[$this->vo->stockDate] = $date;
			$trIndex->children()->each(function($i2, $obj2){
				$obj2 = _($obj2);
				$text = trim($obj2->text());
				if($i2 == 1) $livePriceDt[$this->vo->close] = $this->toPrice($text);
				else if($i2 == 4) $livePriceDt[$this->vo->ref] = round( $livePriceDt[$this->vo->close] - $this->toPrice(substr($text,1)) , 2);
				else if($i2 == 8) $livePriceDt[$this->vo->volume] = $this->toVolume($text)/10;
			});
			$livePriceDt[$this->vo->open] = $livePriceDt[$this->vo->ref];
			if($livePriceDt[$this->vo->open] > $livePriceDt[$this->vo->close]) {
				$livePriceDt[$this->vo->high] = $livePriceDt[$this->vo->open];
				$livePriceDt[$this->vo->low] = $livePriceDt[$this->vo->close];
			} else {
				$livePriceDt[$this->vo->high] = $livePriceDt[$this->vo->close];
				$livePriceDt[$this->vo->low] = $livePriceDt[$this->vo->open];
			}
			
			$livePriceDt[$this->vo->ceil] = $livePriceDt[$this->vo->high];
			$livePriceDt[$this->vo->floor] = $livePriceDt[$this->vo->low];
			$livePriceDt[$this->vo->average] = $livePriceDt[$this->vo->close];
			
			$livePriceDt[$this->vo->buyVolumeP] = null;

			$livePriceDt[$this->vo->buyPrice3] = null;
			$livePriceDt[$this->vo->buyVolume3] = null;
			$livePriceDt[$this->vo->buyPrice2] = null;
			$livePriceDt[$this->vo->buyVolume2] = null;
			$livePriceDt[$this->vo->buyPrice1] = null;
			$livePriceDt[$this->vo->buyVolume1] = null;

			$livePriceDt[$this->vo->sellPrice1] = null;
			$livePriceDt[$this->vo->sellVolume1] = null;
			$livePriceDt[$this->vo->sellPrice2] = null;
			$livePriceDt[$this->vo->sellVolume2] = null;
			$livePriceDt[$this->vo->sellPrice3] = null;
			$livePriceDt[$this->vo->sellVolume3] = null;

			$livePriceDt[$this->vo->sellVolumeP] = null;
						
			if($this->isEqual($livePriceDt, $this->livePriceDtMap[$symbolIndex]) == false){
				$this->livePriceDtMap[$symbolIndex] = $livePriceDt;
				$this->livePriceDtMapSave[$symbolIndex] = $livePriceDt;
			}
			//$__console->log($this->livePriceDtMap[$symbolIndex]);
				
			$table->children()->each(function($i, $obj) {
				$obj = _($obj);
				if($obj->attr("class") == "rowclass"){
					//if($i!=2) return;
					$livePriceDt = array("__new__"=>1);
					$obj->children()->each(function($i2, $obj2){
						$obj2 = _($obj2);
						$text = trim($obj2->text());
						//$__console->log($i2.":".$text);
						if($i2 == 0){
							$text = $obj2->find("span.symbolclass")->text();
							$livePriceDt[$this->vo->stock_symbol] = $text;
							$livePriceDt[$this->vo->stockDate] = $date;
							$livePriceDt[$this->vo->market] = $market;
						}
						else if($i2 == 1) $livePriceDt[$this->vo->ceil] = $this->toPrice($text);
						else if($i2 == 2) $livePriceDt[$this->vo->floor] = $this->toPrice($text);
						else if($i2 == 3) $livePriceDt[$this->vo->ref] = $this->toPrice($text);

						else if($i2 == 4) $livePriceDt[$this->vo->buyVolumeP] = $this->toVolume($text);

						else if($i2 == 5) $livePriceDt[$this->vo->buyPrice3] = $this->toPrice($text);
						else if($i2 == 6) $livePriceDt[$this->vo->buyVolume3] = $this->toVolume($text);
						else if($i2 == 7) $livePriceDt[$this->vo->buyPrice2] = $this->toPrice($text);
						else if($i2 == 8) $livePriceDt[$this->vo->buyVolume2] = $this->toVolume($text);
						else if($i2 == 9) $livePriceDt[$this->vo->buyPrice1] = $this->toPrice($text);
						else if($i2 == 10) $livePriceDt[$this->vo->buyVolume1] = $this->toVolume($text);

						else if($i2 == 17) $livePriceDt[$this->vo->sellPrice1] = $this->toPrice($text);
						else if($i2 == 18) $livePriceDt[$this->vo->sellVolume1] = $this->toVolume($text);
						else if($i2 == 19) $livePriceDt[$this->vo->sellPrice2] = $this->toPrice($text);
						else if($i2 == 20) $livePriceDt[$this->vo->sellVolume2] = $this->toVolume($text);
						else if($i2 == 21) $livePriceDt[$this->vo->sellPrice3] = $this->toPrice($text);
						else if($i2 == 22) $livePriceDt[$this->vo->sellVolume3] = $this->toVolume($text);

						else if($i2 == 23) $livePriceDt[$this->vo->sellVolumeP] = $this->toVolume($text);

						else if($i2 == 26){
							if($market == "ho") $livePriceDt[$this->vo->open] = $this->toPrice($text);
							else if($market == "ha") $livePriceDt[$this->vo->average] = $this->toPrice($text);
						}
						else if($i2 == 24) $livePriceDt[$this->vo->high] = $this->toPrice($text);
						else if($i2 == 25) $livePriceDt[$this->vo->low] = $this->toPrice($text);
						else if($i2 == 14) $livePriceDt[$this->vo->close] = $this->toPrice($text);
						else if($i2 == 16) $livePriceDt[$this->vo->volume] = $this->toVolume($text);
						else if($i2 == 15) $livePriceDt[$this->vo->volume0] = $this->toVolume($text);

						else if($i2 == 27){
							$livePriceDt[$this->vo->buyVolumeF] = $this->toVolume($text);
							if($market == "ho") $livePriceDt[$this->vo->sellVolumeF] = null;
						}
						else if($i2 == 28){
							if($market == "ha") $livePriceDt[$this->vo->sellVolumeF] = $this->toVolume($text);
						}
						if($i2 == 27 || $i2 == 28){
							if($livePriceDt[$this->vo->close] == null) $livePriceDt[$this->vo->close] = $livePriceDt[$this->vo->ref];
							if($livePriceDt[$this->vo->open] == null) $livePriceDt[$this->vo->open] = $livePriceDt[$this->vo->ref];
							if($livePriceDt[$this->vo->high] == null) $livePriceDt[$this->vo->high] = $livePriceDt[$this->vo->ref];
							if($livePriceDt[$this->vo->low] == null) $livePriceDt[$this->vo->low] = $livePriceDt[$this->vo->ref];
							if($livePriceDt[$this->vo->average] == null) $livePriceDt[$this->vo->average] = null;
							if($livePriceDt[$this->vo->volume] == null) $livePriceDt[$this->vo->volume] = 0;
							if($livePriceDt[$this->vo->buyPrice1] == 0) $livePriceDt[$this->vo->buyPrice1] = $livePriceDt[$this->vo->close];
							if($livePriceDt[$this->vo->sellPrice1] == 0) $livePriceDt[$this->vo->sellPrice1] = $livePriceDt[$this->vo->close];

							$symbol = $livePriceDt[$this->vo->stock_symbol];
							if($this->isEqual($livePriceDt, $this->livePriceDtMap[$symbol]) == false){
								$this->livePriceDtMap[$symbol] = $livePriceDt;
								$this->livePriceDtMapSave[$symbol] = $livePriceDt;
							}
						}
					});
				}
			});
		}
		//alert(JSON::stringify($this->livePriceDtMap));
		if(count($this->livePriceDtMapSave)>=0){
			$this->queryReplaceMap($this->livePriceDtMapSave);
		}
	}
	
	public function viewLivePrice(){
		$this->div->css("width", $this->screenW - 17);
		$this->div->css("background-color", "black");
		_($this)->bind("rpc.queryArray", function($event, $result){ $this->onRpc("queryArray", $event, $result); });
		$this->queryArray();
		setInterval(function(){
			$this->queryArray();
		},1500);
		return $this;
	}
	
	public function onRpc($key, $event, $result){
		if("queryArray" == $key){
			$dtMap = $result["return"];
			$analyserConfig = stock_analyser_AnalyserConfig::$config;
			$tableView = $analyserConfig->getBean($analyserConfig->TableView);
			$htmlTotalTable = "&nbsp;";
			if($tableView->tableDtMap!=null) {
				$htmlTotalTable = $tableView->htmlTotalTable();
			}
			$html = $htmlTotalTable."<br/>".$this->htmlLivePrice($dtMap);
			$this->div->html($html);
		}
	}
	
	public function htmlLivePrice($dtMap){
?>
<$html>
<table border=1 cellpadding="0" cellspacing="0" style="position:absolute;left:0px; border-color:#333333;border-collapse:collapse;width:100%;">
<tr style="background-color:#DBDBDB;height:20">
	<td>value</td>
	
	<td><b>avg%</b></td>
	<td><b>symbol</b></td>
	<td><b>per%</b></td>
	
	<td><b>per%</b></td>
	<td><b>symbol</b></td>
	<td><b>avg%</b></td>
	
	<td>value</td>
	
	<td>buy3</td>
	<td>buyVol3</td>
	<td>buy2</td>
	<td>buyVol2</td>
	<td>buy1</td>
	<td>buyVol1</td>
	
	<td><b>close</b></td>
	<td><b>per%</b></td>
	<td><b>symbol</b></td>
	<td><b>perB&nbsp;</b></td>
	<td><b>per1&nbsp;</b></td>
	<td><b>per12</b></td>
	<td>vol</td>
	<td><b>avg%</b></td>
	<td>value</td>
	<td>buyx1</td>
	
	<td>sell1</td>
	<td>sellVol1</td>
	<td>sell2</td>
	<td>sellVol2</td>
	<td>sell3</td>
	<td>sellVol3</td>

	<td>low%&nbsp;</td>
	<td>high%</td>
	
</tr>
<?
$length = count($dtMap);
if($length%2==1) $length = $length+1;
for($i=0; $i<$length/2; $i++){
	$tr = '<tr style="text-align:right;">';
	$tr .= $this->htmlTrLivePrice($dtMap[$length/2 + $i], $i, "left");
	$tr .= $this->htmlTrLivePrice($dtMap[$i], $i, "right");
	$tr .= '</tr>';
?>
		<?= $tr ?>
<?
}
?>

<?
//$length = count($dtMap);
for($i=0; $i<$length/2; $i++){
	$tr = '<tr style="text-align:right;">';
	$tr .= $this->htmlTrLivePrice($dtMap[$i], $i, "left-no");
	$tr .= $this->htmlTrLivePrice($dtMap[$length/2 + $i], $i, "right");
	$tr .= '</tr>';
?>
		<?= $tr ?>
<?
}
?>

</table>
</$html>
<?
		return $html;
	}
	
	public function htmlLivePrice2($dtMap){
?>
<$html>
<table border=1 cellpadding="0" cellspacing="0" style="border-color:#505050;border-collapse:collapse;width:100%">
<tr style="background-color:#DBDBDB;height:20">
	<td>symbol</td>
	<td>ref</td>
	<td>ceil</td>
	<td>floor</td>

	<td>buy3</td>
	<td>buyVol3</td>
	<td>buy2</td>
	<td>buyVol2</td>
	<td>buy1</td>
	<td>buyVol1</td>

	<td>close</td>
	<td><b>per%</b></td>
	<td><b>symbol&nbsp;&nbsp;&nbsp;</b></td>
	<td><b>perB</b></td>
	<td><b>per1&nbsp;</b></td>
	<td><b>per12</b></td>
	<td>vol</td>
	<td><b>avg</b></td>
	<td>value</td>
	<td>buyx1</td>

	<td>sell1</td>
	<td>sellVol1</td>
	<td>sell2</td>
	<td>sellVol2</td>
	<td>sell3</td>
	<td>sellVol3</td>

	<td>high</td>
	<td>low</td>
</tr>
<?
$count = 0;
foreach($dtMap as $symbol => $dt){
	$count++;
?> <?= $this->htmlTrLivePrice2($dt, $count) ?>
<? } ?>
</table>
</$html>
<?
		return $html;
	}
	
	public function htmlLivePriceShort($dtMap){
		$vo = new stock_price_LivePriceVo();
		$stockColor = '#00FF00';
		$bgColor1 = "#444444";
		$dtVn = $dtMap["VNINDEX"];
		$dtHa = $dtMap["HAINDEX"];
?>
<$html>
<table border=1 cellpadding="0" cellspacing="0" style="border-color:gray;border-collapse:collapse;width:100%">
<?
$count = 0;
foreach($dtMap as $symbol => $dt){
	$count++;
	if(1==0 && ($symbol=="VNINDEX" || $symbol=="HAINDEX")){
	} else {
?> <?= $this->htmlTrLivePrice2($dt, $count) ?>
<?
	}
}
?>
</table>
</$html>
<?
		return $html;
	}
	
	public function htmlTrLivePrice($dt, $count, $positon){
		if($dt==null) $dt = array("__new__"=>1);
		$vo = new stock_price_LivePriceVo();
		$analyserConfig = stock_analyser_AnalyserConfig::$config;
		$tableView = $analyserConfig->getBean($analyserConfig->TableView);
		$bgColor1 = "";
		if($count%2==0) $bgColor1 = "#444444";
		else $bgColor1 = "#111111";
		$bgColor2 = "";
		if($count%2==0) $bgColor2 = "#223344";
		else $bgColor2 = "#112233";
		$bgColor1 = "#222222";
		$bgColor1 = "black";
		$bgColor2 = "#222222";
		//$bgColor2 = $bgColor1;
		//$bgColor2 = "#334455";
		$tableDt = null;
		if($tableView->tableDtMap!=null) $tableDt = $tableView->tableDtMap[$dt["stock_symbol"]];
		if($tableDt == null) $tableDt = array();
		$stockColor = $tableView->getStockColor($tableDt);
		$getStockStyle = $tableView->getStockStyle($tableDt);
		//$getStockStyle = "normal";
		
		if($positon == "left"){
?>
<$html>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>;border-right-color:none"><?= $this->formatValue($vo->getLiveValue($dt)) ?></td>
	<?
	/*
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->sellPrice1], $dt) ?>"><?= $dt[$vo->sellPrice1] ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:white;border-right-color:red"><?= $this->formatVolume($dt[$vo->sellVolume1]) ?></td>
	
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>"><?= $this->formatPrice($dt[$vo->close]) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:white;border-right-color:red"><?= $this->formatVolume($dt[$vo->volume]) ?></td>

	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->buyPrice1], $dt) ?>"><?= $dt[$vo->buyPrice1] ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:white;border-right-color:red"><?= $this->formatVolume($dt[$vo->buyVolume1]) ?></td>
	*/
	?>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->average], $dt) ?>"><?= $this->formatPrice($this->getAvgPercent($dt)) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $stockColor ?>;<?= $getStockStyle ?>;"><a tag="aSymbol" href="" onclick="return false;" style="color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>;"><?= $dt[$vo->stock_symbol] ?></a></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>"><?= $this->formatPrice($this->getPricePercent($dt[$vo->close], $dt)) ?></td>
</$html>
<?
		}
		else if($positon == "left-no"){
?>
<$html>
	<td style="background-color:<?= $bgColor1 ?>;">&nbsp;</td>


	<td style="background-color:<?= $bgColor1 ?>;">&nbsp;</td>
	<td style="background-color:<?= $bgColor1 ?>;">&nbsp;</td>
	<td style="background-color:<?= $bgColor1 ?>;">&nbsp;</td>
</$html>
<?
		}
		else if($positon == "right"){
?>
<$html>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>"><?= $this->formatPrice($this->getPricePercent($dt[$vo->close], $dt)) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $stockColor ?>;text-align:left;<?= $getStockStyle ?>"><a tag="aSymbol" href="" onclick="return false;" style="color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>;"><?= $dt[$vo->stock_symbol] ?></a></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->average], $dt) ?>;border-right-color:none;"><?= $this->formatPrice($this->getAvgPercent($dt)) ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>;border-right-color:none">
		<? if($dt[$vo->stock_symbol] != "VNINDEX" && $dt[$vo->stock_symbol] != "HAINDEX") { ?> <?= $this->formatValue($vo->getLiveValue($dt)) ?> <? } ?>
	</td>
	
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->buyPrice3], $dt) ?>"><?= $dt[$vo->buyPrice3] ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:white"><?= $this->formatVolume($dt[$vo->buyVolume3]) ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->buyPrice2], $dt) ?>"><?= $dt[$vo->buyPrice2] ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:white"><?= $this->formatVolume($dt[$vo->buyVolume2]) ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->buyPrice1], $dt) ?>"><?= $dt[$vo->buyPrice1] ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:white;border-right-color:none"><?= $this->formatVolume($dt[$vo->buyVolume1]) ?></td>
	
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>;"><?= $this->formatPrice($dt[$vo->close]) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>"><?= $this->formatPrice($this->getPricePercent($dt[$vo->close], $dt)) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $stockColor ?>;text-align:left;<?= $getStockStyle ?>"><a tag="aSymbol" href="" onclick="return false;" style="color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>;"><?= $dt[$vo->stock_symbol] ?></a></td>
	
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->sellPrice1], $dt) ?>"><?= $this->formatPrice($this->getPricePercent($dt[$vo->sellPrice1], $dt)) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPerColor($tableDt["maPer1"]) ?>"><?= $this->formatPrice($tableDt["maPer1"]) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPerColor($tableDt["maPer12"]) ?>"><?= $this->formatPrice($tableDt["maPer12"]) ?></td>
	
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>"><?= $this->formatVolume($dt[$vo->volume0]) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->average], $dt) ?>"><?= $this->formatPrice($this->getAvgPercent($dt)) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>;border-right-color:none"><?= $this->formatValue($vo->getLiveValue($dt)) ?></td>	
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->sellPrice1], $dt) ?>"><?= $this->formatVolume(($tableView->capPer)*100*round( 0.1*0.1*$tableView->capital/$dt[$vo->sellPrice1])) ?></td>
	
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->sellPrice1], $dt) ?>"><?= $dt[$vo->sellPrice1] ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:white"><?= $this->formatVolume($dt[$vo->sellVolume1]) ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->sellPrice2], $dt) ?>"><?= $dt[$vo->sellPrice2] ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:white"><?= $this->formatVolume($dt[$vo->sellVolume2]) ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->sellPrice3], $dt) ?>"><?= $dt[$vo->sellPrice3] ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:white"><?= $this->formatVolume($dt[$vo->sellVolume3]) ?></td>
	
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->low], $dt) ?>;"><?= $this->formatPrice($this->getPricePercent($dt[$vo->low], $dt)) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->high], $dt) ?>;"><?= $this->formatPrice($this->getPricePercent($dt[$vo->high], $dt)) ?></td>

</$html>
<?
		}
		return $html;
	}
	
	public function htmlTrLivePrice2($dt, $count){
		if($dt==null) $dt = array("__new__"=>1);
		$vo = new stock_price_LivePriceVo();
		$analyserConfig = stock_analyser_AnalyserConfig::$config;
		$tableView = $analyserConfig->getBean($analyserConfig->TableView);
		$bgColor1 = "";
		if($count%2==0) $bgColor1 = "#444444";
		else $bgColor1 = "#111111";
		$bgColor2 = "";
		if($count%2==0) $bgColor2 = "#223344";
		else $bgColor2 = "#112233";
		$bgColor1 = "#222222";
		$bgColor1 = "black";
		$bgColor2 = "#222222";
		//$bgColor2 = $bgColor1;
		//$bgColor2 = "#334455";
		$tableDt = null;
		if($tableView->tableDtMap!=null) $tableDt = $tableView->tableDtMap[$dt["stock_symbol"]];
		if($tableDt == null) $tableDt = array();
		$stockColor = $tableView->getStockColor($tableDt);
		$getStockStyle = $tableView->getStockStyle($tableDt);
		//$getStockStyle = "normal";
			
?>
<$html>
<tr style="text-align:right;">
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $stockColor ?>;text-align:left;<?= $getStockStyle ?>"><a tag="aSymbol" href="" onclick="return false;" style="color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>;"><?= $dt[$vo->stock_symbol] ?></a></td>
	<td style="background-color:<?= $bgColor2 ?>;color:#F7FF31"><?= $dt[$vo->ref] ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:#FF00FF"><?= $dt[$vo->ceil] ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:#66CCFF"><?= $dt[$vo->floor] ?></td>
	
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->buyPrice3], $dt) ?>"><?= $dt[$vo->buyPrice3] ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->buyPrice3], $dt) ?>"><?= $this->formatVolume($dt[$vo->buyVolume3]) ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->buyPrice2], $dt) ?>"><?= $dt[$vo->buyPrice2] ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->buyPrice2], $dt) ?>"><?= $this->formatVolume($dt[$vo->buyVolume2]) ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->buyPrice1], $dt) ?>"><?= $dt[$vo->buyPrice1] ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->buyPrice1], $dt) ?>;border-right-color:none"><?= $this->formatVolume($dt[$vo->buyVolume1]) ?></td>
	
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>;"><?= $this->formatPrice($dt[$vo->close]) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>"><?= $this->formatPrice($this->getPricePercent($dt[$vo->close], $dt)) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $stockColor ?>;text-align:left;<?= $getStockStyle ?>"><a tag="aSymbol" href="" onclick="return false;" style="color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>;"><?= $dt[$vo->stock_symbol] ?></a></td>
	
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->sellPrice1], $dt) ?>"><?= $this->formatPrice($this->getPricePercent($dt[$vo->sellPrice1], $dt)) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPerColor($tableDt["maPer1"]) ?>"><?= $this->formatPrice($tableDt["maPer1"]) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPerColor($tableDt["maPer12"]) ?>"><?= $this->formatPrice($tableDt["maPer12"]) ?></td>
	
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>"><?= $this->formatVolume($dt[$vo->volume0]) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->average], $dt) ?>"><?= $this->formatPrice($this->getAvgPercent($dt)) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->close], $dt) ?>;border-right-color:none"><?= $this->formatValue($vo->getLiveValue($dt)) ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->sellPrice1], $dt) ?>"><?= $this->formatVolume(($tableView->capPer)*100*round( 0.1*0.1*$tableView->capital/$dt[$vo->sellPrice1])) ?></td>
	
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->sellPrice1], $dt) ?>"><?= $dt[$vo->sellPrice1] ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->sellPrice1], $dt) ?>"><?= $this->formatVolume($dt[$vo->sellVolume1]) ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->sellPrice2], $dt) ?>"><?= $dt[$vo->sellPrice2] ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->sellPrice2], $dt) ?>"><?= $this->formatVolume($dt[$vo->sellVolume2]) ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->sellPrice3], $dt) ?>"><?= $dt[$vo->sellPrice3] ?></td>
	<td style="background-color:<?= $bgColor1 ?>;color:<?= $this->getPriceColor($dt[$vo->sellPrice3], $dt) ?>"><?= $this->formatVolume($dt[$vo->sellVolume3]) ?></td>
	
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->high], $dt) ?>"><?= $dt[$vo->high] ?></td>
	<td style="background-color:<?= $bgColor2 ?>;color:<?= $this->getPriceColor($dt[$vo->low], $dt) ?>"><?= $dt[$vo->low] ?></td>
</tr>
</$html>
<?
		return $html;
	}
	
	public function formatVolume($volume){
		if($volume==null || $volume==0) return null;
		return number_format($volume,0,".",",");
		//return number_format($volume/1000,1,".",",");
	}
	
	public function formatValue($value){
		if($value==null) return null;
		return number_format($value/1000000,1,".",",");
	}
	
	public function formatPrice($price){
		if($price==null || $price=="") return null;
		return number_format($price,2,".",",");
	}
	
	public function getPriceColor($price, $dt){
		//$per = 100*($price - $dt["ref"])/$dt["ref"];
		//if($per >= 3 ) return "#FF00FF";
		//if($per <= -3 ) return "#66CCFF";
		
		if($price == $dt["ceil"]) return "#E100E1";
		if($price == $dt["floor"]) return "#66CCFF";
		//if($price == $dt["ref"]) return "#F7FF31";
		if($price > $dt["ref"]) return "lime";
		if($price <= $dt["ref"]) return "red";
		
		if($price == $dt["ceil"]) return "#E100E1";
		if($price == $dt["floor"]) return "#66CCFF";
		//if($price == $dt["ref"]) return "#F7FF31";
		if($price > $dt["ref"]) return "#00D700";
		if($price <= $dt["ref"]) return "#EC0000";
		return "#F7FF31";
	}
	public function getPerColor($per){
		if($per > 2 ) return "lime";
		return "red";
	}
	
	public function getMarketColor($dt){
		return "white";
		return "#00FF00";
		if($dt["market"] == "ho") return "#00FF00";
		if($dt["market"] == "ha") return "#FF0000";
		return "#F7FF31";
	}
	
	public function getPricePercent($price, $dt){
		if($price==null) return "";
		$str = "";
		if($price >= $dt["ref"]) $str .= "+";
		$str .= round( 100*($price - $dt["ref"])/$dt["ref"], 2);
		return $str;
	}
	
	public function getAvgPercent($dt){
		if($dt["average"]==null || $dt["average"]=="" || $dt["average"]==0) return null;
		$str = "";
		if($dt["average"] >= $dt["ref"]) $str .= "+";
		$str .= round( 100*($dt["average"] - $dt["ref"])/$dt["ref"], 2);
		return $str;
	}

	public function toPrice($str){
		if($str==null || trim($str)=="") return null;
		return doubleval($str);
	}
	public function toVolume($str){
		if($str==null || trim($str)=="") return null;
		$str = str_replace(",", "", $str);
		return 10*doubleval($str);
	}
	public function toDate($str){
		if($str==null || trim($str)=="") return null;
		$arr = explode("/", $str);
		$arr2 = array();
		$arr2[0] = $arr[2];
		$arr2[1] = substr("0".$arr[1], -2);
		$arr2[2] = substr("0".$arr[0], -2);
		$str = implode("-", $arr2);
		return $str;
	}

	public function isEqual($obj1, $obj2){
		if($obj1==null && $obj2==null) return true;
		if($obj1==null || $obj2==null) return false;
		foreach($obj1 as $i=>$value){
			if($value != $obj2[$i]) return false;
		}
		return true;
	}
	
	public function queryMapAll(){
		return $this->loadJson($this->livePriceDtMapFile);
	}
	
	public function queryMapMapAll(){
		return $this->loadJson($this->livePriceDtMapMapFile);
	}
	
	public function queryMap(){
		//<@ $this->jsonp();
		$livePriceDtMapJson = $this->loadJson($this->livePriceDtMapFile);
		
		$tableView = new stock_analyser_TableView();
		$tableDtArrayMap = $this->loadJson($tableView->tableDtArrayMapFile);
		$return = array();
		$return["VNINDEX"] = $livePriceDtMapJson["VNINDEX"];
		$return["HAINDEX"] = $livePriceDtMapJson["HAINDEX"];
		foreach($tableDtArrayMap as $symbol=>$tableDt){
			if($symbol == "TOTAL") continue;
			$return[$symbol] = $livePriceDtMapJson[$symbol];
		}
		return $return;
		//@>
	}
	
	public function queryArray(){
		//<@ $this->jsonp();
		$livePriceDtMapJson = $this->loadJson($this->livePriceDtMapFile);
		
		$tableView = new stock_analyser_TableView();
		$tableDtArrayMap = $this->loadJson($tableView->tableDtArrayMapFile);
		$count = 0;
		$return = array();
		$return[$count++] = $livePriceDtMapJson["VNINDEX"];
		$return[$count++] = $livePriceDtMapJson["HAINDEX"];
		foreach($tableDtArrayMap as $symbol=>$tableDt){
			if($symbol == "TOTAL") continue;
			$return[$count++] = $livePriceDtMapJson[$symbol];
		}
		return $return;
		//@>
	}
	
	public function queryMapBySymbol($symbol){
		//<@ $this->jsonp();
		$livePriceDtMapJson = $this->loadJson($this->livePriceDtMapFile);
		$return = array();
		$return["VNINDEX"] = $livePriceDtMapJson["VNINDEX"];
		$return["HAINDEX"] = $livePriceDtMapJson["HAINDEX"];
		$return[$symbol] = $livePriceDtMapJson[$symbol];
		return $return;
		//@>
	}

	protected  $stmCreate;
	public function queryCreate() {
		//<@ $this->jsonp();
		if($this->stmCreate == null){
			$vo = $this->vo;
			$sql = "CREATE TABLE ".get_class($vo)."(
			stock_symbol VARCHAR(255),
			stockDate DATETIME,
			market VARCHAR(255),
			open DOUBLE,
			high DOUBLE,
			low DOUBLE,
			close DOUBLE,
			volume INT,
			
			volume0 INT,
	
			ceil DOUBLE,
			floor DOUBLE,
			ref DOUBLE,

			buyPrice3 DOUBLE,
			buyVolume3 INT,
			buyPrice2 DOUBLE,
			buyVolume2 INT,
			buyPrice1 DOUBLE,
			buyVolume1 INT,

			sellPrice1 DOUBLE,
			sellVolume1 INT,
			sellPrice2 DOUBLE,
			sellVolume2 INT,
			sellPrice3 DOUBLE,
			sellVolume3 INT,
			
			buyVolumeP INT,
			sellVolumeP INT,
	
			average DOUBLE,
			buyVolumeF INT,
			sellVolumeF INT,
	
			PRIMARY KEY( stock_symbol, stockDate )
			) TYPE=MyISAM CHARACTER SET utf8";
			$this->stmCreate = $this->ds->prepare($sql);
		}
		$this->stmCreate->execute();
		//@>
	}

	protected $stmReplace;
	public function queryReplace($livePriceDt){
		//<@ $this->jsonp($livePriceDt);
		$vo = $this->vo;
		if($this->stmReplace == null){
			$sql = "REPLACE INTO ".get_class($vo)." SET ";
			foreach ($vo as $key=>$value){
				if(substr($key,0,2) == "__") continue;
				$sql .= "$key =:$value,";
			}
			$sql = substr($sql, 0, -1) . ";";
			$this->stmReplace = $this->ds->prepare($sql);
		}
		foreach ($vo as $key=>$value){
			if($livePriceDt[$key]==null) $livePriceDt[$key] = null;
		}
		$dt = array();
		foreach ($livePriceDt as $key=>$value){
			if(substr($key,0,2) == "__") continue;
			$dt[$key] = $livePriceDt[$key];
		}
		$this->stmReplace->execute($dt);
		//@>
	}

	public $livePriceDtMapFile = "WEB-INF/files/stock/price/livePriceDtMap.json";
	public $livePriceDtMapMapFile = "WEB-INF/files/stock/price/livePriceDtMapMap.json";
	public function queryReplaceMap($livePriceDtMap){
		//<@ $this->jsonPost($livePriceDtMap);
		$livePriceDtMapJson = $this->loadJson($this->livePriceDtMapFile);
		if($livePriceDtMapJson==null) $livePriceDtMapJson = array();
		foreach ($livePriceDtMap as $symbol=>$livePriceDt){
			//$this->queryReplace($livePriceDt);
			$livePriceDtMapJson[$symbol] = $livePriceDt;
		}
		$this->saveJson($this->livePriceDtMapFile, $livePriceDtMapJson);
		//////return;
		
		$MINUTE = 30;
		$time = time();
		date_default_timezone_set("Asia/Ho_Chi_Minh");
		$arrDate = getDate($time);
		//print_p($arrDate);
		
		$saving = true;
		
		if( $arrDate['wday'] == 0 || $arrDate['wday'] == 6 ||
			 ( $arrDate['hours'] + $arrDate['minutes']/60 < 8 + 35/60 ) || 
			 ( $arrDate['hours'] + $arrDate['minutes']/60 > 11 + 30/60 )
		){
			$saving = false;
		}
		
		if( $arrDate['hours'] + $arrDate['minutes']/60 <= 8 + 30/60){
			$arrDate['hours'] = 8;
			$arrDate['minutes'] = 30;
		}
		
		if( $arrDate['hours'] + $arrDate['minutes']/60 >= 11){
			$arrDate['hours'] = 10;
			$arrDate['minutes'] = 59;
		}
		
		if($saving){

			$arrDate['seconds'] = 0;
			$arrDate['minutes'] = (int)(floor($arrDate['minutes']/$MINUTE) * $MINUTE);
			//$arrDate['seconds'] = (int)(floor($arrDate['seconds']/30) * 30);
			$time = mktime($arrDate['hours'], $arrDate['minutes'], $arrDate['seconds'], $arrDate['mon'], $arrDate['mday'], $arrDate['year']);	
			$strDate = date('Y-m-d H:i:s', $time);
			$strDatePrev = date('Y-m-d H:i:s', $time - $MINUTE*60);
			//print_p($strDate);
			//print_p($strDatePrev);
			
			$livePriceDtMapMapJson = $this->loadJson($this->livePriceDtMapMapFile);
			foreach ($livePriceDtMapMapJson as $date=>$livePriceDtMap){
				if(substr($date, 0, 10) != substr($strDate, 0 , 10)) unset($livePriceDtMapMapJson[$date]);
			}

			if($livePriceDtMapMapJson==null) $livePriceDtMapMapJson = array();
			if( $livePriceDtMapMapJson[$strDate] == null)  $livePriceDtMapMapJson[$strDate] = array();

			$vo = $this->vo;
			foreach ($livePriceDtMapJson as $symbol=>$livePriceDt){
				$livePriceDt[$vo->stockDate] = $strDate;
				
				if($livePriceDt[$vo->stock_symbol]==null) continue;
				if($livePriceDt[$vo->close]==null || !is_numeric($livePriceDt[$vo->close])  || $livePriceDt[$vo->close]==0) $livePriceDt[$vo->close] = $livePriceDt[$vo->ref];
				if($livePriceDt[$vo->open]==null || !is_numeric($livePriceDt[$vo->open])  || $livePriceDt[$vo->open]==0) $livePriceDt[$vo->open] = $livePriceDt[$vo->close];
				
				$livePriceDtPrev = $livePriceDtMapMapJson[$strDatePrev][$symbol];
				$_livePriceDt = $livePriceDtMapMapJson[$strDate][$symbol];
				
				if($_livePriceDt == null){
					$livePriceDt[$vo->open] = $livePriceDt[$vo->close];
					$livePriceDt[$vo->high] = $livePriceDt[$vo->close];
					$livePriceDt[$vo->low] = $livePriceDt[$vo->close];
					//$this->dbDao->insertLivePrice($livePriceVo);
					if($livePriceDtPrev!=null) {
						//$livePriceDt["__volume"] = $livePriceDt[$vo->volume];
						//if($livePriceDtPrev["__volume"]!=null) $livePriceDt[$vo->volume] = $livePriceDt[$vo->volume] - $livePriceDtPrev["__volume"];
					}
					$this->queryReplace($livePriceDt);
					$livePriceDtMapMapJson[$strDate][$symbol] = $livePriceDt;
				} else {
					$livePriceDt[$vo->open] = $_livePriceDt[$vo->open];
					$livePriceDt[$vo->low] = $_livePriceDt[$vo->low];
					$livePriceDt[$vo->high] = $_livePriceDt[$vo->high];
					if($livePriceDt[$vo->low] == null || $livePriceDt[$vo->close] < $livePriceDt[$vo->low]) $livePriceDt[$vo->low] = $livePriceDt[$vo->close];
					if($livePriceDt[$vo->high] == null || $livePriceDt[$vo->close] > $livePriceDt[$vo->high]) $livePriceDt[$vo->high] = $livePriceDt[$vo->close];
					if($livePriceDt[$vo->open]==null) $livePriceDt[$vo->open] = $livePriceDt[$vo->close];
					//$this->dbDao->updateLivePrice($livePriceVo);
					if($livePriceDtPrev!=null){
						//$livePriceDt["__volume"] = $livePriceDt[$vo->volume];
						//if($livePriceDtPrev["__volume"]!=null) $livePriceDt[$vo->volume] = $livePriceDt[$vo->volume] - $livePriceDtPrev["__volume"];
					}
					$this->queryReplace($livePriceDt);
					$livePriceDtMapMapJson[$strDate][$symbol] = $livePriceDt;
				}
			}
			/*
			function sortFunc($a, $b){
				if(date($a) > date($b)) return 1;
				else return -1;
				return 0;
			}
			uksort($livePriceDtMapMapJson, "sortFunc");
			*/
			$this->saveJson($this->livePriceDtMapMapFile, $livePriceDtMapMapJson);
		}
		//@>
	}

	public function web(){
		//<@
		$headers = array();
		foreach($_SERVER as $i=>$val) {
			if (strpos($i, 'HTTP_') === 0) {
				//if($i == "HTTP_PRAGMA") continue;
				//if($i == "HTTP_CACHE_CONTROL") continue;
				//if($i == "HTTP_COOKIE") continue;

				$array = explode("_", $i);
				array_shift($array);
				foreach ($array as $i=>$value){
					$array[$i] = ucwords(strtolower($value));
				}
				$name = implode("-", $array);
				$headers[$name] = $val;
			}
		}
		$url = $_SERVER["REDIRECT_URL"];
		$url =  substr($url, strpos($url, "/web/") + 5);
		$Host =  substr($url, 0, strpos($url, "/"));
		$url = "http://" . $url;
		if($headers["Referer"]!=null){
			$Referer = $headers["Referer"];
			$Referer = substr($Referer, strpos($Referer, "/web/") + 5);
			$Referer = "http://" . $Referer;
			$headers["Referer"] = $Referer;
		}
		if($headers["Host"]!=null) $headers["Host"] = $Host;

		if($_SERVER['REQUEST_METHOD']=="POST"){
			//print_p($headers);return;
		}

		$filename = substr($url, strrpos($url, "/")+1);
		if($filename=="") $filename="hose";
		$filename = "web/".$filename;
		if(1==0 && file_exists($filename)){
			//$html = file_get_contents($filename);
		} else {
			$httpClient = new Zend_Http_Client($url);
			//$Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*'.'/'.'*;q=0.8';
			$httpClient->setHeaders($headers);
			$response = $httpClient->request($_SERVER['REQUEST_METHOD']);
			$html = $response->getBody();
			$ContentType = $response->getHeader("Content-type");
			if($ContentType!=null){
				header("Content-Type: ".$ContentType);
			}
		}

		echo $html;
		//@>
	}
	
	public static function main(){
		_($__document)->ready(function(){
			$config = stock_price_PriceConfig::$config;
			$view = $config->getBean($config->LivePriceView);
			_($__document->body)->append($view->div);
			$view->view();
		});
	}
}